/*servertest.c*/
/*assume that the server is machine 0*/
#include "syscall.h"
#include "syscall2.h"

int myLocks[1024];
int myConds[1024];
int myMVs[1024];

int main()
{	
	int firstLock = 0;
	int firstCond = 0;
	int MV_W1 = 0;
	int MV_W2 = 0;
	int MV_S1 = 0;
	int MV_S2 = 0;
	int i = 0;
	int r=0;
	int c=0;
	int test=0;
	int outputLen = 0;
	int failed = 0;
	char outBuff[32];
	
	ConsoleAcquire();
		Write("Starting signal client...\n",26,ConsoleOutput);
	ConsoleRelease();
	
	firstLock = CreateLock("first",5);
	firstCond = CreateCondition("first",5);
	
	ConsoleAcquire();
		Write("Lock/Cond = ",12,ConsoleOutput);
		outputLen = Itoa(firstLock, outBuff);
		Write(outBuff , outputLen, ConsoleOutput);
		Write("/",1,ConsoleOutput);
		outputLen = Itoa(firstCond, outBuff);
		Write(outBuff , outputLen, ConsoleOutput);
		Write("\n",1,ConsoleOutput);
	ConsoleRelease();
	
	Acquire(firstLock);
	Signal(firstCond,firstLock);
	ConsoleAcquire();
		Write("Signal client releasing lock...\n",32,ConsoleOutput);
	ConsoleRelease();
	Release(firstLock);/*WAIT takes control*/
	
	Acquire(firstLock);/*SIGNAL has control*/
	
	/*MV Tests p2*/
	/*create and set own MVs*/
	MV_S1 = CreateMV(2,2,5,"MV_S1");
	MV_S2 = CreateMV(2,2,5,"MV_S2");
	
	SetMV(MV_S1,0,0,1);
	SetMV(MV_S1,0,1,1);
	SetMV(MV_S1,1,0,1);
	SetMV(MV_S1,1,1,1);
	SetMV(MV_S2,0,0,2);
	SetMV(MV_S2,0,1,2);
	SetMV(MV_S2,1,0,2);
	SetMV(MV_S2,1,1,2);
	
	/*Get and Test WAIT's MVs*/
	MV_W1 = CreateMV(2,2,5,"MV_W1");
	MV_W2 = CreateMV(2,2,5,"MV_W2");
	
	if(GetMV(MV_W1,0,0) != 1) failed = 1;
	if(GetMV(MV_W1,0,1) != 1) failed = 1;
	if(GetMV(MV_W1,1,0) != 1) failed = 1;
	if(GetMV(MV_W1,1,1) != 1) failed = 1;
	if(GetMV(MV_W2,0,0) != 2) failed = 1;
	if(GetMV(MV_W2,0,1) != 2) failed = 1;
	if(GetMV(MV_W2,1,0) != 2) failed = 1;
	if(GetMV(MV_W2,1,1) != 2) failed = 1;
	
	if(failed)
	{
		ConsoleAcquire();
			Write("Failed testing other server MVs!\n",33,ConsoleOutput);
		ConsoleRelease();
		Exit(0);
	}
	
	Release(firstLock); /*To MV Tests part 3*/
	
	Acquire(firstLock); /*MV Tests part 4*/
	Release(firstLock); /*Final Release*/
	
	/*Destroy all MVs*/
	DestroyMV(MV_W1);
	DestroyMV(MV_W2);
	DestroyMV(MV_S1);
	DestroyMV(MV_S2);
	
	DestroyLock(firstLock);
	DestroyCondition(firstCond);
	
	ConsoleAcquire();
		Write("Server test complete!\n",22,ConsoleOutput);
	ConsoleRelease();
	Exit(0);
}
